package com.hjli.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;

public class PropertiesTool {

	private static Properties prop = null;

	
	public static Properties getProperty(String propertyName) throws Exception{
		prop = new Properties();
		InputStream inputstream = null;
		ClassLoader cl = PropertiesTool.class.getClassLoader();
		// System.out.println(cl);
		if (cl != null) {
			inputstream = cl.getResourceAsStream(propertyName);
			
		} else {
			inputstream = ClassLoader.getSystemResourceAsStream(propertyName);
		}

		if (inputstream == null) {
			throw new Exception("inputstream " + propertyName + " open null");
		}
		prop.load(inputstream);
		inputstream.close();
		inputstream = null;
		return prop;
	}
	
	/**
	 * 初始化Properties实例
	 * 
	 * @param propertyName
	 * @throws Exception
	 */
	public synchronized static void initProperty(String propertyName) throws Exception {
		if (prop == null) {
			prop = new Properties();
			InputStream inputstream = null;
			ClassLoader cl = PropertiesTool.class.getClassLoader();
			// System.out.println(cl);
			if (cl != null) {
				inputstream = cl.getResourceAsStream(propertyName);
			} else {
				inputstream = ClassLoader.getSystemResourceAsStream(propertyName);
			}

			if (inputstream == null) {
				throw new Exception("inputstream " + propertyName + " open null");
			}
			prop.load(inputstream);
			inputstream.close();
			inputstream = null;
		}
	}

	/**
	 * 读取数据
	 * 
	 * @param propertyName
	 * @param key
	 * @return
	 */
	public static String getValueByKey(String propertyName, String key) {
		String result = "";
		try {
			initProperty(propertyName);
			result = prop.getProperty(key);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 修改properties的值
	 */
	public static Boolean updatePro(String path, String key, String value) {
		Properties prop = new Properties();// 属性集合对象
		FileInputStream fis;
		try {
			fis = new FileInputStream(path);
			prop.load(fis);// 将属性文件流装载到Properties对象中
			fis.close();// 关闭流
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		prop.setProperty(key, value);
		// 文件输出流
		try {
			FileOutputStream fos = new FileOutputStream(path);
			// 将Properties集合保存到流中
			prop.store(fos,null);
			fos.close();// 关闭流
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean updateProperty(String filename,String key,String value){
        FileInputStream fis = null;
        Properties properties = new Properties();
        try {
            fis = new FileInputStream(filename);
            BufferedReader bf = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
            properties.load(bf);
            OutputStream out = new FileOutputStream(filename);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
            properties.setProperty(key, value);
            properties.store(bw, null);
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
